/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      AAAServiceImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-8-8 下午05:12:22
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.rmi.server.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.isoftstone.industry.aaa.support.rmi.IAAAService;
import com.isoftstone.industry.aaa.support.rmi.exception.AAARemoteException;
import com.isoftstone.industry.aaa.core.service.accounting.IChargeOffService;
import com.isoftstone.industry.aaa.core.service.accounting.IPayService;
import com.isoftstone.industry.aaa.exception.AAAException;
import com.isoftstone.industry.aaa.exception.BalanceNotEnoughException;

/**
 * @author jacky
 */
@Component("aaaService")
public class AAAServiceImpl implements IAAAService {
	private Logger logger = Logger.getLogger(AAAServiceImpl.class);

	@Resource
	private IChargeOffService chargeOffService;

	@Resource
	private IPayService payService;

	@Override
	@Transactional
	public void chargeOff4Recharge(String userId) {
		chargeOffService.chargeOff4Recharge(userId);
	}

	@Override
	public String pay4Order(String orderId) throws AAARemoteException {
		try {
			return payService.pay4Order(orderId);
		} catch (AAAException e) {
			throw newRemoteException(e);
		}
	}

	private AAARemoteException newRemoteException(AAAException e) {
		logger.error("AAARemoteException: errorCode = " + e.getErrorCode());
		AAARemoteException result = null;
		if (e instanceof BalanceNotEnoughException) {
			result = new com.isoftstone.industry.aaa.support.rmi.exception.BalanceNotEnoughException(
					((BalanceNotEnoughException) e).getPayableAmount(),e.getArgs(), e
					.getMessage(), e.getCause());
		} else {
			result = new AAARemoteException(e.getErrorCode(), e.getArgs(), e
					.getMessage(), e.getCause());
		}
		return result;
	}

	@Override
	public void pay4Bill(String billId) {
		payService.pay4Bill(billId);
	}

}
